package com.szewec.ps.ui.road.facilities.intersection.task.templete;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.szewec.ps.R;
import com.szewec.ps.base.ListBaseAdapter;
import com.szewec.ps.base.SuperViewHolder;
import com.szewec.ps.util.ListUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 复制进口道的dialog。样式请参考路口设施调查任务页面，如果你需要的样式跟这个dialog相似，可以自行修改后使用
 */
public class CheckDialog extends Dialog {

    /**
     * 显示的标题
     */
    private TextView tvTitle;

    private Button btnCancel, btnDetermine;

    private LRecyclerView lrvImportSelect;


    private String title;
    private String leftBtn, rightBtn;
    /**
     * 底部是否只有一个按钮
     */
    private boolean isSingle = false;
    /**
     * 按钮之间的分割线
     */
    private View columnLineView;

    private MyAdapter myAdapter;
    private List<?> mList = new ArrayList<>();
    private List<Object> targetList = new ArrayList<>();

//    private int position = -1;

    private final Context context;

    public CheckDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_import_options);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //设置背景透明
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //初始化界面控件
        initView();

        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        btnCancel = findViewById(R.id.btn_cancel);
        btnDetermine = findViewById(R.id.btn_determine);
        tvTitle = findViewById(R.id.tv_title);
        lrvImportSelect = findViewById(R.id.l_recycler_view_import_select);
        columnLineView = findViewById(R.id.column_line);
        if (context != null) {
            myAdapter = new MyAdapter(context);
            myAdapter.setDataList((Collection<Object>) mList);
            lrvImportSelect.setAdapter(new LRecyclerViewAdapter(myAdapter));
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            lrvImportSelect.setLayoutManager(linearLayoutManager);
            lrvImportSelect.setLoadMoreEnabled(false);
            lrvImportSelect.setPullRefreshEnabled(false);
        }
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        btnDetermine.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onDetermineClick();
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        btnCancel.setOnClickListener(v -> {
            if (onClickBottomListener != null) {
                onClickBottomListener.onCancelClick();
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(rightBtn)) {
            btnDetermine.setText(rightBtn);
        } else {
            btnDetermine.setText("确定");
        }
        if (!TextUtils.isEmpty(leftBtn)) {
            btnCancel.setText(leftBtn);
        } else {
            btnCancel.setText("取消");
        }
        /**
         * 只显示一个按钮的时候隐藏取消按钮，回掉只执行确定的事件
         */
        if (isSingle) {
            columnLineView.setVisibility(View.GONE);
            btnCancel.setVisibility(View.GONE);
        } else {
            btnCancel.setVisibility(View.VISIBLE);
            columnLineView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void show() {
        super.show();
        //初始化界面数据
        refreshView();
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;

    public CheckDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    public interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        void onDetermineClick();

        /**
         * 点击取消按钮事件
         */
        void onCancelClick();
    }

    private class MyAdapter extends ListBaseAdapter<Object> {


        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_import_options;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            Object str = mDataList.get(position);
            TextView content = holder.getView(R.id.tv_content);
            content.setText(str.toString());
            ImageView imageView = holder.getView(R.id.iv_checked);

            if (ListUtil.isNotEmpty(targetList)) {
                for (Object o : targetList) {
                    if (str.toString().equals(o.toString())) {
                        imageView.setBackgroundResource(R.mipmap.ic_select);
                    }
                }
            } else {
                targetList = new ArrayList<>();
            }

            holder.itemView.setOnClickListener(v -> {
                //判断选择的选项是否存在输出list中，存在则删除，不存在则添加
                Object o = mDataList.get(position);
                if (targetList.contains(o)) {
                    targetList.remove(o);
                    imageView.setBackgroundResource(R.mipmap.ic_no_select);
                } else {
                    targetList.add(o);
                    imageView.setBackgroundResource(R.mipmap.ic_select);
                }
            });
        }
    }

    public String getTitle() {
        return title;
    }

    public CheckDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getLeftBtn() {
        return leftBtn;
    }

    public CheckDialog setLeftBtn(String leftBtn) {
        this.leftBtn = leftBtn;
        return this;
    }

    public String getRightBtn() {
        return rightBtn;
    }

    public CheckDialog setRightBtn(String rightBtn) {
        this.rightBtn = rightBtn;
        return this;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CheckDialog setSingle(boolean single) {
        isSingle = single;
        return this;
    }

    public List<?> getMList() {
        return mList;
    }

    /**
     * 设置列表清单
     *
     * @param mList
     * @return
     */
    public CheckDialog setMList(List<?> mList) {
        this.mList = mList;
//        if (mList != null) {
//            myAdapter.setDataList(mList);
//            myAdapter.notifyDataSetChanged();
//        }
        return this;
    }

    /**
     * 设置回显选中list
     *
     * @param targetList
     * @return
     */
    public CheckDialog setTargetList(List<?> targetList) {
        this.targetList = (List<Object>) targetList;
        return this;
    }

    public List<?> getTargetList() {
        return targetList;
    }

    /**
     * 选项点击回调
     */
    public interface OnViewClick {
        void onClick(List<?> list);
    }

    /**
     * @param list  需要显示的清单
     * @param click 点击确定后的回调
     */
    public void showCheck(List<?> list, OnViewClick click) {
        setOnClickBottomListener(new OnClickBottomListener() {
            @Override
            public void onDetermineClick() {
                click.onClick(targetList);
                dismiss();
            }

            @Override
            public void onCancelClick() {
                dismiss();
            }
        });
        setMList(list).show();
    }
}
